//
//  CTMLeftViewController.m
//  CallTaxi
//
//  Created by yuan fang on 12-10-9.
//  Copyright (c) 2012年 MoPay. All rights reserved.
//

#import "CTMLeftViewController.h"
#import "CTMainViewController.h"
//#import "YPConfig.h"
#import "CLImage.h"

#define kChangeMiddleViewNotification @"changeMiddleView"



@interface CTMLeftViewController ()

@end

@implementation CTMLeftViewController

@synthesize baseTableView;
@synthesize dataArray;

- (void)loadView {
    
    BOOL isiPhone5 = ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO);
    
    // BaseView
    UIView *baseView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.view = baseView;
    
    // BaseTableView
    UITableView *tempTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)
                                                              style:UITableViewStylePlain];
    if (isiPhone5) {
        tempTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320,568)
                                                     style:UITableViewStylePlain];
    }
    
    self.baseTableView = tempTableView;
    
    self.baseTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.baseTableView.dataSource = self;
    self.baseTableView.delegate = self;
    
    
    UIImageView *backImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"menu_main_bg.png"]];

    if (isiPhone5) {
        backImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"menu_main_bg_ip5.png"]];
    }

    self.baseTableView.backgroundView = backImageView;
        
    self.baseTableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.baseTableView];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    if (!dataArray) {
        self.dataArray = [NSMutableArray arrayWithObjects:@"技术讨论区",
                          @"新时代的我们",
                          @"达盖尔的旗帜",
                          @"文学区",
                          @"亚洲无码原创",
                          @"亚洲有码原创",
                          @"亚洲无码转帖",
                          @"亚洲有码转帖",
                          @"欧美原创",
                          @"欧美转帖",
                          nil];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        
        // Cell选中背景
        UIImage *cellSelectImage = [UIImage imageNamed:kMenuCellImage];
        cellSelectImage = [cellSelectImage stretchableImageWithLeftCapWidth:2 topCapHeight:2];
        UIImageView *cellSelectBackView = [[UIImageView alloc] initWithImage:cellSelectImage];
        [cell setSelectedBackgroundView:cellSelectBackView];
        
        // 图标
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(12, 12, 16, 20)];
        imageView.contentMode = UIViewContentModeCenter;
        imageView.tag = 101;
        [cell addSubview:imageView];
        
        // 信息
        UILabel *menuItemLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 12, 150, 20)];
        menuItemLabel.backgroundColor = [UIColor clearColor];
        menuItemLabel.textColor = [UIColor colorWithWhite:1.0 alpha:1];
        menuItemLabel.font = [UIFont systemFontOfSize:16];
        menuItemLabel.tag = 102;
        [cell addSubview:menuItemLabel];
        
        // 箭头
        UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(235, 12, 16, 20)];
        arrowImageView.contentMode = UIViewContentModeCenter;
        arrowImageView.image = [UIImage imageNamed:kMenuArrowImage];
        [cell addSubview:arrowImageView];
        
        // 分割线
        UIImageView *cellLineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 43, 280, 1)];
        cellLineImageView.contentMode = UIViewContentModeCenter;
        cellLineImageView.tag = 103;
        [cell addSubview:cellLineImageView];
    }
    
    // 图标
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:101];
    switch (indexPath.row) {
        case 0:
//            imageView.image = [UIImage imageNamed:kMenuCoverImage];
            break;
        case 1:
//            imageView.image = [UIImage imageNamed:kMenuMessageImage];
            break;
        case 2:
//            imageView.image = [UIImage imageNamed:kMenuMineImage];
            break;
        default:
            break;
    }
    
    // 信息
    UILabel *menuItemLabel = (UILabel *)[cell viewWithTag:102];
    menuItemLabel.text = [dataArray objectAtIndex:indexPath.row];
    
    // 分割线
    UIImageView *cellLineImageView = (UIImageView *)[cell viewWithTag:103];
    cellLineImageView.image = [UIImage imageNamed:kMenuLineImage];
    
    return cell;
}

#pragma mark -
#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableDictionary *notificationDic = [NSMutableDictionary dictionary];
    [notificationDic setObject:indexPath forKey:@"type"];
    [[NSNotificationCenter defaultCenter] postNotificationName:kChangeMiddleViewNotification object:nil userInfo:notificationDic];
}
@end
